﻿using DTCash.Logger;
using DTCash.Logger.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTCash.Redis;
using Uranus.Common;
using Uranus.Service.RepaymentService;

namespace Uranus.Service
{
    public class Repayment : BaseData
    {

        private static Repayment repayment = null;

        public static Repayment GetRepayment()
        {
            if (repayment == null)
            {
                repayment = new Repayment();
            }
            return repayment;
        }

        /// <summary>
        /// 获取还款数据
        /// </summary>
        /// <param name="repaymentItemFormRequest"></param>
        /// <returns></returns>
        public RepaymentItemFormResponse GetRepaymentItemFormDTOList(RepaymentItemFormRequest request)
        {
            RePaymentServiceClient client = new RePaymentServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(request.OrderItemID.ToString(), "GetRepaymentItemFormDTOList").Repayment.Info;
                Func<RepaymentItemFormResponse> method = delegate
                {
                    RepaymentItemFormResponse res = client.GetRepaymentItemFormDTOList(request);
                    return res.Code == 1 ? res : null;
                };
                return base.GetData<RepaymentItemFormResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Repayment.GetRepaymentItemFormDTOList 出现异常", ex.Message.ToString(), request);

                return null;
            }
            finally
            {
                client.Close();
            }
        }

        #region
        /// <summary>
        /// 还贷计划，按任意金额贷款
        /// </summary>
        /// <param name="money">总金额</param>
        /// <param name="rate">利率</param>
        /// <param name="month">周期</param>
        /// <param name="method">还款方式</param>
        /// <returns></returns>
        public RepaymentScheduleDTO RepaymentSchedule(decimal money, decimal rate, int month, int method)
        {
            RePaymentServiceClient client = new RePaymentServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity("RepaymentSchedule", money, rate, month, method).Repayment.Info;
                Func<RepaymentScheduleDTO> methodFun = delegate
                {
                    return client.RepaymentSchedule(money, rate, month, method);
                };
                return base.GetData<RepaymentScheduleDTO>(methodFun, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.Repayment.RepaymentSchedule 出现异常", ex.Message.ToString(), new { money = money, rate = rate, month = month, method = method });

                return null;
            }
            finally
            {
                client.Close();
            }
        }
        #endregion
        
        /// <summary>
        /// 活期产品的主动还款操作（卖出操作）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public CurrentProductRepaymentResponse SellOutCurrentProduct(CurrentProductRepaymentRequest request)
        {
            RePaymentServiceClient client = new RePaymentServiceClient();
            try
            {
                return client.SellOutCurrentProduct(request);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogError("活期产品卖出接口异常  SellOutCurrentProduct", CodeCommon.Serialize(request), ex, true);
                return new CurrentProductRepaymentResponse() {Code = 2};
            }
            finally
            {
                client.Close();
            }
        }
    }
}
